//
//  UINoticeTextViewUtil.m
//  Wewe
//
//  Created by Jessiegan on 11-8-10.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "UINoticeTextUtil.h"
#import "ZDUIUtil.h"

#define  MaxBorderWidth  SCREEN_BOUNDS.size.width-50
#define  DefaultBorderWidth  SCREEN_BOUNDS.size.width-80
#define  DefaultBorderHeigtht  40
#define  imageViewWidth  12
#define  imageViewLeft  8

@interface Notice_Queue : NSObject {
    NSString *noticeStr;
    float yOffset;
    NSTimeInterval duration;
    
}
@property (nonatomic,copy) NSString *noticeStr;
@property (nonatomic,assign) float yOffset;
@property (nonatomic,assign) NSTimeInterval duration;
@property (nonatomic,assign) ZDTipsLevel tipsLevel;


@end
@implementation Notice_Queue
@synthesize noticeStr,duration,yOffset;

-(id)init
{
    self = [super init];
    if (self != nil) {
        yOffset = 0.;
        duration = 0.;
    }
    return self;
}
@end

UINoticeTextUtil *noticeTextUtil = nil;
#define animatedDuration 0.5
@implementation UINoticeTextUtil
@synthesize lastText;
-(id)init
{
    self = [super init];
    if (self != nil) {
//        YOffset = 0.f;
        isShow = NO;
        showBoardView = nil;
        noticeTextView = nil;
        self.lastText = nil;

        noticeQueue = [[NSOperationQueue alloc] init];
        [noticeQueue setMaxConcurrentOperationCount:1];
    }
    return self;
}

+(UINoticeTextUtil *)sharedInstance
{
    if (noticeTextUtil == nil) {
        noticeTextUtil = [[UINoticeTextUtil alloc] init];
    }
    return noticeTextUtil;
}

- (void) resetFrame
{
    showBoardView.frame = CGRectMake(SCREEN_BOUNDS.size.width/16.0f, (SCREEN_BOUNDS.size.height*(400.0f))/480.0f, 0, 0);
    noticeTextView.frame = CGRectMake(SCREEN_BOUNDS.size.width/32.0f, SCREEN_BOUNDS.size.height/48.0f, 0, 0);
}


- (void) setBoardFrame:(float)yOffset
{
    CGRect boardFrame = noticeTextView.frame;
    boardFrame.origin.x = (SCREEN_BOUNDS.size.width-(noticeTextView.frame.size.width+SCREEN_BOUNDS.size.width/16.0f))/2.0f;
    boardFrame.origin.y = (SCREEN_BOUNDS.size.height*(400.0f+yOffset))/480.0f-noticeTextView.frame.size.height;
    boardFrame.size.width += SCREEN_BOUNDS.size.width/16.0f;
    boardFrame.size.height += SCREEN_BOUNDS.size.height/24.0f;
    showBoardView.frame = boardFrame; 
    showBoardView.alpha = 0.0;
}

-(void)processData:(id)data
{
    if (data == nil || ![data isKindOfClass:[Notice_Queue class]]) {
        return;
    }
    [self performSelectorOnMainThread:@selector(showWithNotice:) withObject:data waitUntilDone:YES];
    NSTimeInterval duration = [(Notice_Queue*)data duration];
    if (duration > 0) {
        [NSThread sleepForTimeInterval:duration+animatedDuration];
        [self performSelectorOnMainThread:@selector(endShow) withObject:nil waitUntilDone:YES];
        [NSThread sleepForTimeInterval:animatedDuration];
    }
}

-(void)showWithNotice:(Notice_Queue *)notice
{
    NSString *text = [(Notice_Queue*)notice noticeStr];
    if (isShow || text == nil) {
        return;
    }
    float yoffset = [(Notice_Queue*)notice yOffset];
    ZDTipsLevel  tipsLevle=[(Notice_Queue*)notice  tipsLevel];
    
    
    if (noticeTextView == nil)
    {
        noticeTextView = [[UILabel alloc] initWithFrame:CGRectMake(imageViewWidth+imageViewLeft*2, 0, 0, 0)];
        noticeTextView.backgroundColor = [UIColor clearColor];
        noticeTextView.textColor = [UIColor whiteColor];
        noticeTextView.numberOfLines = 0;
        noticeTextView.font=[UIFont systemFontOfSize:14];
        noticeTextView.lineBreakMode = NSLineBreakByCharWrapping;
    }
    if (imageTips==nil) {
        imageTips=[[UIImageView  alloc]   initWithFrame:CGRectMake(imageViewLeft, 0, imageViewWidth, imageViewWidth)];
        BUTTONCORNER(imageTips,imageViewWidth/2);
        imageTips.backgroundColor=THEME_NAVIGATIONBAR_COLOR;
       
    }
    UIImage  *image=tipsLevle==ZDTipsLevelNotice?[UIImage imageNamed:@"icon_toast_error_icon"]:[UIImage imageNamed:@"icon_toast_success"];
    
    imageTips.image=image;

    if (viewNotice==nil) {
        
        viewNotice=[[UIView alloc] init];
        [viewNotice  addSubview:noticeTextView];
        [viewNotice  addSubview:imageTips];

    }
       //        添加背景;
    if (imageBG== nil) {
        imageBG=[[UIImageView alloc]  init];
        imageBG.alpha=0.3;
        imageBG.backgroundColor=[UIColor  blackColor];
       
    }
    if(showBoardView == nil)
    {
        showBoardView = [[UIView alloc] init];
        showBoardView.backgroundColor = [UIColor clearColor];
        ROUNDVIEW(showBoardView);
        [showBoardView addSubview:imageBG];
        [showBoardView addSubview:viewNotice];
//        [showBoardView addSubview:noticeTextView];
        showBoardView.userInteractionEnabled = NO;
    
    }
//    [self resetFrame];
    
    noticeTextView.text = text;
//宽度以最大的宽度
    [ZDUIUtil autoAdjustLabel:noticeTextView maxWidth:MaxBorderWidth-imageViewLeft*2-imageViewWidth-10 type:autoAdjustLeftTop];
    [self  resetViewFrame:yoffset];
    
    lastWindow = [UIApplication sharedApplication].windows.lastObject;
    lastWindowEnabled=lastWindow.userInteractionEnabled;
    lastWindow.userInteractionEnabled=NO;
    [lastWindow addSubview:showBoardView];
   
    
    NSTimeInterval durationSec = animatedDuration;
    [UIView beginAnimations:@"Fade" context:nil];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDuration:durationSec];
    showBoardView.alpha = 1.0;
    [UIView commitAnimations];
    /*
    if (secs > 0) 
    {
        [NSTimer scheduledTimerWithTimeInterval:secs+durationSec target:self selector:@selector(endShow) userInfo:nil repeats:NO];
    }*/
    isShow = YES;
}
-(void)resetViewFrame:(float  )yoffset{

    [viewNotice setSizeV:CGSizeMake(noticeTextView.widthV+2*imageViewLeft+imageViewWidth+10, noticeTextView.heightV)];
    //    重新布局，设置尺寸
    if (viewNotice.Width<DefaultBorderWidth) {
        [showBoardView  setWidthV:DefaultBorderWidth];
    }
    else{
        [showBoardView setWidthV:viewNotice.Width];
    }
    
    if (viewNotice.Height<DefaultBorderHeigtht) {
        [showBoardView setHeightV:DefaultBorderHeigtht+10];;
    }
    else{
        [showBoardView setHeightV:viewNotice.Height+10];
    }
    
    [showBoardView  setCenterXV:SCREEN_WIDTH/2];
    [showBoardView  setCenterYV:SCREEN_HEIGHT-yoffset-showBoardView.heightV/2];
    
    [viewNotice setCenterXV: showBoardView.centerxV];
    [viewNotice setCenterYV: showBoardView.centeryV];
    
    imageTips.centerYV=viewNotice.centeryV;
    
    //   imageBG 尺寸赋值
    imageBG.frame=showBoardView.bounds;
    

}
- (void)showWithText:(NSString *)text stay:(NSTimeInterval)secs offset:(CGFloat)offset TipsLevel:(ZDTipsLevel)tipsLevel{

    if (text == nil
        || [self.lastText isEqualToString:text]
        || [text isKindOfClass:[NSNull class]])
    {
        return;
    }
    else
    {
        self.lastText = [NSString stringWithString:text];
    }
    Notice_Queue *note = [[Notice_Queue alloc] init];
    note.noticeStr = text;
    note.duration = secs;
    note.yOffset = offset;
    note.tipsLevel=tipsLevel;
    
    Operation *noticeOperation = [[Operation alloc] initWithData:note delegate:self];
    [noticeQueue addOperation:noticeOperation];
    [noticeOperation release];
    [note release];
    
}
- (void)showWithText:(NSString *)text stay:(NSTimeInterval)secs offset:(CGFloat)offset; // 0 to stay forever
{
//    默认状态
    [self  showWithText:text stay:secs offset:offset TipsLevel:ZDTipsLevelNotice];
    
}
- (void)showWithText:(NSString *)text stay:(NSTimeInterval)secs
{
    [self showWithText:text stay:secs offset:0];
}

- (void)endShow
{
    NSTimeInterval durationSec = animatedDuration;
    [UIView beginAnimations:@"Fade" context:nil];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDuration:durationSec];
    [UIView setAnimationDidStopSelector:@selector(fadeOut)];
    showBoardView.alpha = 0.0;
    [UIView commitAnimations];
    self.lastText = nil;
}
-(void)setLastWindow{


}


-(void)fadeOut
{
    
    
    [showBoardView removeFromSuperview];
   
    NSArray   *arrWindows = [UIApplication sharedApplication].windows;
    [arrWindows  enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        UIWindow  *window=obj;
        if ([window isEqual:lastWindow] ) {
            window.userInteractionEnabled=lastWindowEnabled;
        }
        ;
    }];
    
    
    isShow = NO;
}

- (void)dealloc
{
    [noticeQueue release];
    if (showBoardView != nil) {
        [showBoardView release];
    }
    if (noticeTextView != nil) {
        [noticeTextView release];
    }   
    self.lastText = nil;
    [super dealloc];
}

@end
